<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <title>AJ Utils-ObjectHelper 教程</title>
        <meta name="description" content="Small Java utilities with many useful functions.  为处理 Java 对象提供了实用工具方法" />
        <meta name="keywords" content="AJ Utils, ajaxjs, ajaxjs framework, java utilities, tools, helper, 对象,工具,克隆" />
        <meta name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="preconnect" href="https://fonts.googleapis.com" />
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
        <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@200..900&family=Noto+Serif:ital,wght@0,100..900;1,100..900&display=swap&family=Noto+Sans+SC:wght@100..900&display=swap" />
        <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@100..900&family=Noto+Serif:ital,wght@0,100..900;1,100..900&display=swap" /> 
        <link rel="stylesheet" href="https://framework.ajaxjs.com/static/new-ui/css/common.css" />
        <link rel="stylesheet" href="https://iam.ajaxjs.com/asset/main.css"/>
        <link rel="icon" type="image/x-icon" href="https://framework.ajaxjs.com/aj-logo/logo.ico"/>
        <script src="https://framework.ajaxjs.com/static/aj-docs/common.js"></script>
        <script>
            var _hmt = _hmt || [];
            (function() {
              var hm = document.createElement("script");
              hm.src = "https://hm.baidu.com/hm.js?208c5aea11c52991bd1c3283e62ad0ce";
              var s = document.getElementsByTagName("script")[0];
              s.parentNode.insertBefore(hm, s);
            })();
        </script>
    </head>
    <body>
        <nav>
            <div>
                <div class="links">
                    <a href="/cn">🏠 首页</a>
                    | ⚙️ 源码:
                    <a target="_blank" href="https://github.com/lightweight-component/aj-util">Github</a>/<a target="_blank" href="https://gitcode.com/lightweight-component/aj-util">Gitcode</a>
                  
                    | 🚀<a href="">英语版本</a>
                </div>
                <h1>
                    <img src="https://framework.ajaxjs.com/aj-logo/logo.png" style="vertical-align: middle;height: 45px;margin-bottom: 6px;" />
                    AJ Utils
                </h1>
                <h3>小型、干净、简单的 Java 工具库
                </h3>
            </div>
        </nav>
        <div>
            <menu>
                
                <ul>
                    <li class="selected">
                        <a href="/cn">首页</a>
                    </li>
                </ul>
                <h3>Common 常用模块</h3>
                <ul>
                    <li><a href="/common/JsonUtil-cn/">JsonUtil</a></li>
                    <li><a href="/common/Base64Helper-cn/">Base64Helper</a></li>
                    <li><a href="/common/ConvertBasicValue-cn/">ConvertBasicValue</a></li>
                    <li><a href="/common/MessageDigestHelper-cn/">HashHelper</a></li>
                    <li><a href="/common/MapTool-cn/">MapTool</a></li>
                    <li><a href="/common/ObjectHelper-cn/">ObjectHelper</a></li>
                    <li><a href="/common/RandomTools-cn/">RandomTools</a></li>
                    <li><a href="/common/RegExpUtils-cn/">RegExpUtils</a></li>
                    <li><a href="/common/StrUtil-cn/">StrUtil</a></li>
                    <li><a href="/common/BoxLogger-cn/">BoxLogger</a></li>
                    <li><a href="/common/BytesHelper-cn/">BytesHelper</a></li>
                    <li><a href="/common/EncodeTools-cn/">UrlEncode</a></li>
                    <li><a href="/common/XmlHelper-cn/">XmlHelper</a></li>
                    <li><a href="/common/WebUtils-cn/">WebUtils</a></li>
                </ul>
                <h3>Date 日期处理</h3>
                <ul>
                    <li><a href="/date/intro-cn/">简介</a></li>
                    <li><a href="/date/convert-cn/">万能日期类型转换</a></li>
                    <li><a href="/date/formatter-cn/">日期格式化</a></li>
                </ul>
                <h3>Reflection 反射</h3>
                <ul>
                    <li><a href="/reflect/Methods-cn/">Methods</a></li>
                    <li><a href="/reflect/Types-cn/">Types</a></li>
                    <li><a href="/reflect/Clazz-cn/">Clazz</a></li>
                </ul>

                <h3>Input/Output 输入/输出</h3>
                <ul>
                    <li><a href="/io/FileHelper-cn/">FileHelper</a></li>
                    <li><a href="/io/Resources-cn/">Resources</a></li>
                    <li><a href="/io/StreamHelper-cn/">DataReader/DataWriter</a></li>
                    <li><a href="/io/ZipHelper-cn/">ZipHelper</a></li>
                </ul>

                <h3>HTTP 请求</h3>
                <ul>
                    <li><a href="/http_request/Get-cn">HTTP 请求</a></li>
                     <li><a href="/http_request/advanced-usage-cn/">高级用法</a></li>
                    <li><a href="/http_request/Base-cn/">Base</a></li>
                </ul>

                <h3>加密/解密</h3>
                <ul>
                    <li><a href="/cryptography/intro-cn/">简介</a></li>
                    <li><a href="/cryptography/flow-cn/">基本流程</a></li>
                    <li><a href="/cryptography/AesCrypto-cn/">AES/DES 加密解密</a></li>
                    <li><a href="/cryptography/RsaCrypto-cn/">RSA 加密解密</a></li>
                </ul>
            </menu>
            <article class="aj-text chinese">
                <h1>ObjectHelper 使用教程</h1>
<p><code>ObjectHelper</code> 是一个对象工具类，提供了处理 Java 对象、集合、映射和各种数据结构的通用工具方法。</p>
<h3>主要功能特性</h3>
<ol>
<li><strong>文本内容检查和验证</strong></li>
<li><strong>集合和数组空值检查</strong></li>
<li><strong>创建集合的便捷工厂方法（映射、列表、集合）</strong></li>
<li><strong>容量计算工具</strong></li>
</ol>
<h3>文本检查方法</h3>
<h4>1. hasText 方法</h4>
<p>检查给定字符串是否有实际文本内容：</p>
<pre><code class="language-java">// 检查字符串是否包含非空白字符
boolean result1 = ObjectHelper.hasText(&quot;Hello&quot;);     // true
boolean result2 = ObjectHelper.hasText(&quot;   &quot;);       // false
boolean result3 = ObjectHelper.hasText(&quot;&quot;);          // false
boolean result4 = ObjectHelper.hasText(null);        // false
</code></pre>
<h4>2. isEmptyText 方法</h4>
<p>检查给定字符串是否为空或仅包含空白字符：</p>
<pre><code class="language-java">// 检查字符串是否为空或仅包含空白字符
boolean result1 = ObjectHelper.isEmptyText(&quot;Hello&quot;); // false
boolean result2 = ObjectHelper.isEmptyText(&quot;   &quot;);   // true
boolean result3 = ObjectHelper.isEmptyText(&quot;&quot;);      // true
boolean result4 = ObjectHelper.isEmptyText(null);    // true
</code></pre>
<h3>空值检查方法</h3>
<h4>1. 数组空值检查</h4>
<pre><code class="language-java">// 检查数组是否为空
String[] array1 = null;
String[] array2 = new String[0];
String[] array3 = {&quot;Hello&quot;};

boolean result1 = ObjectHelper.isEmpty(array1); // true
boolean result2 = ObjectHelper.isEmpty(array2); // true
boolean result3 = ObjectHelper.isEmpty(array3); // false
</code></pre>
<h4>2. 集合空值检查</h4>
<pre><code class="language-java">// 检查集合是否为空
List&lt;String&gt; list1 = null;
List&lt;String&gt; list2 = new ArrayList&lt;&gt;();
List&lt;String&gt; list3 = Arrays.asList(&quot;Hello&quot;);

boolean result1 = ObjectHelper.isEmpty(list1); // true
boolean result2 = ObjectHelper.isEmpty(list2); // true
boolean result3 = ObjectHelper.isEmpty(list3); // false
</code></pre>
<h4>3. 映射空值检查</h4>
<pre><code class="language-java">// 检查映射是否为空
Map&lt;String, String&gt; map1 = null;
Map&lt;String, String&gt; map2 = new HashMap&lt;&gt;();
Map&lt;String, String&gt; map3 = new HashMap&lt;&gt;();
map3.put(&quot;key&quot;, &quot;value&quot;);

boolean result1 = ObjectHelper.isEmpty(map1); // true
boolean result2 = ObjectHelper.isEmpty(map2); // true
boolean result3 = ObjectHelper.isEmpty(map3); // false
</code></pre>
<h3>集合创建方法</h3>
<h4>1. 创建映射</h4>
<pre><code class="language-java">// 创建单键值对映射
Map&lt;String, String&gt; map1 = ObjectHelper.mapOf(&quot;key1&quot;, &quot;value1&quot;);

// 创建双键值对映射
Map&lt;String, String&gt; map2 = ObjectHelper.mapOf(&quot;key1&quot;, &quot;value1&quot;, &quot;key2&quot;, &quot;value2&quot;);

// 创建三键值对映射
Map&lt;String, String&gt; map3 = ObjectHelper.mapOf(&quot;key1&quot;, &quot;value1&quot;, &quot;key2&quot;, &quot;value2&quot;, &quot;key3&quot;, &quot;value3&quot;);

// 创建指定容量的映射
Map&lt;String, String&gt; map4 = ObjectHelper.mapOf(10); // 优化容量的HashMap
</code></pre>
<h4>2. 创建列表</h4>
<pre><code class="language-java">// 创建不可变列表
List&lt;String&gt; list1 = ObjectHelper.listOf(&quot;item1&quot;);
List&lt;String&gt; list2 = ObjectHelper.listOf(&quot;item1&quot;, &quot;item2&quot;, &quot;item3&quot;);
List&lt;String&gt; list3 = ObjectHelper.listOf(); // 空列表
</code></pre>
<h4>3. 创建集合</h4>
<pre><code class="language-java">// 创建不可变集合
Set&lt;String&gt; set1 = ObjectHelper.setOf(&quot;item1&quot;);
Set&lt;String&gt; set2 = ObjectHelper.setOf(&quot;item1&quot;, &quot;item2&quot;, &quot;item3&quot;);
Set&lt;String&gt; set3 = ObjectHelper.setOf(); // 空集合

// 自动去重
Set&lt;String&gt; set4 = ObjectHelper.setOf(&quot;item1&quot;, &quot;item1&quot;, &quot;item2&quot;); // 只包含&quot;item1&quot;和&quot;item2&quot;
</code></pre>
<h3>常量</h3>
<ul>
<li><code>EMPTY_PARAMS_MAP</code>: 空的不可变映射，当不需要参数时可用作默认值或标记值</li>
<li><code>DEFAULT_LOAD_FACTOR</code>: 哈希映射的默认负载因子，针对时间和空间效率进行了优化</li>
</ul>
<h3>注意事项</h3>
<ol>
<li><code>listOf</code> 和 <code>setOf</code> 方法创建的是不可变集合</li>
<li><code>setOf</code> 方法不允许包含 null 元素</li>
<li><code>mapOf</code> 方法会根据预期大小优化初始容量，减少重新调整大小的操作</li>
</ol>

            </article>
        </div>

       <footer>
            AJ-Util，开源框架 <a href="https://framework.ajaxjs.com" target="_blank">AJ-Framework</a> 的一部分。联系方式：
            frank@ajaxjs.com，<a href="https://blog.csdn.net/zhangxin09" target="_blank">作者博客</a>
            <br />
            <br />
            Copyright © 2025 Frank Cheung. All rights reserved.
        </footer>
    </body>
</html>